/*
 * @Author: panzhiwei 749458928@qq.com
 * @Date: 2024-01-24 15:40:12
 * @LastEditors: panzhiwei 749458928@qq.com
 * @LastEditTime: 2024-02-01 18:40:02
 * @FilePath: /2DDemo/assets/Script/Enemy/Enemy.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// Enemy.ts
import { _decorator, Component, Vec3, Prefab, instantiate, ParticleSystem, Label, tween, find, UITransform, UIOpacity } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Component {
    @property
    health: number = 100; // 敌人的生命值

    @property
    targetY: number = -300; // 敌人移动的目标 Y 坐标

    @property
    public moveSpeed: number = 1; // 敌人移动速度

    @property
    public AttackPower: number = 3; // 敌人攻击力

    @property({ type: Prefab })
    explosionEffectPrefab: Prefab = null;

    @property({ type: Prefab })
    damageLabelPrefab: Prefab = null; // 攻击数值 Label 预制体

    private isDestroyed: boolean = false; // 标记敌人是否已被销毁

    // 敌人受伤的方法
    public takeDamage(damage: number, position: Vec3) {
        this.health -= damage;
        if (this.health <= 0 && !this.isDestroyed) {
            this.isDestroyed = true; // 标记敌人为已销毁
            this.createExplosionEffect(position);
            // 生命值小于等于 0，敌人消失
            setTimeout(() => {
                this.node.removeFromParent();
                this.node.destroy();
            }, 1000); // 假设爆炸持续1秒

        }
        this.showDamage(damage);
    }

    // 敌人受伤的方法
    public explosionTakeDamage(damage: number):boolean {
        this.health -= damage;
        if (this.health <= 0 && !this.isDestroyed) {
            this.isDestroyed = true; // 标记敌人为已销毁
            // this.createExplosionEffect(position);
            // 生命值小于等于 0，敌人消失
            setTimeout(() => {
                this.node.removeFromParent();
                this.node.destroy();
            }, 1000); // 假设爆炸持续1秒
            this.showDamage(damage);
            return true;
        }
        this.showDamage(damage);
        return false;
    }

    // // 火箭弩受伤后
    // public rocketExplosionTakeDamage(damage: number, position: Vec3):boolean {
    //     this.health -= damage;
    //     if (this.health <= 0 && !this.isDestroyed) {
    //         this.isDestroyed = true; // 标记敌人为已销毁
    //         // this.createExplosionEffect(position);
    //         // 生命值小于等于 0，敌人消失
    //         setTimeout(() => {
    //             this.node.removeFromParent();
    //             this.node.destroy();
    //         }, 1000); // 假设爆炸持续1秒
    //         this.showDamage(damage);
    //         return true;
    //     }
    //     this.showDamage(damage);
    //     return false;
    // }

    update(deltaTime: number) {
        if (!this.isDestroyed) { // 只有在敌人未被销毁时才更新位置
            this.updateEnemyPosition(deltaTime);
        }
    }

    updateEnemyPosition(deltaTime: number) {
        let pos = this.node.position;
        let newY = pos.y - this.moveSpeed * deltaTime;
        if (newY <= this.targetY) {
            // 到达或超过目标位置，可以在这里处理敌人到达底部后的逻辑
            this.node.removeFromParent();
            this.node.destroy();
        } else {
            this.node.setPosition(new Vec3(pos.x, newY, pos.z));
        }
    }

    // 可以添加更多敌人的行为方法...
    createExplosionEffect(position: Vec3) {
        const explosion = instantiate(this.explosionEffectPrefab);
        explosion.setParent(this.node.parent); // 确保设置正确的父节点
        explosion.setPosition(position);

        // 播放爆炸效果，例如启动粒子系统
        const particleSystem = explosion.getComponent(ParticleSystem);
        if (particleSystem) {
            particleSystem.play();
        }

        // 可以选择在一定时间后销毁爆炸效果对象
        setTimeout(() => {
            explosion.destroy();
        }, 1000); // 假设爆炸持续1秒
    }

    showDamage(damage: number) {
        const damageLabelNode = instantiate(this.damageLabelPrefab);
        const canvas = find('Canvas');
        if (canvas) {
            damageLabelNode.setParent(canvas);

            const uiTransform = canvas.getComponent(UITransform);
            const worldPos = this.node.worldPosition.add(new Vec3(0, this.node.worldScale.y / 2 + 20, 0));
            const canvasPos = uiTransform.convertToNodeSpaceAR(worldPos);
            damageLabelNode.setPosition(canvasPos);
        }

        const label = damageLabelNode.getComponent(Label);
        if (label) {
            label.string = damage.toString();
        }

        const uiOpacity = damageLabelNode.getComponent(UIOpacity) || damageLabelNode.addComponent(UIOpacity);
        tween(uiOpacity)
            .to(1, { opacity: 0 }) // 渐隐到透明
            .call(() => {
                damageLabelNode.destroy();
            })
            .start();
    }
}